<!DOCTYPE html>
<html class="um landscape min-width-240px min-width-320px min-width-480px min-width-768px min-width-1024px">
    <head>
        <title></title>
        <meta charset="utf-8">
        <meta name="viewport" content="target-densitydpi=device-dpi, width=device-width, initial-scale=1, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <link rel="stylesheet" href="css/fonts/font-awesome.min.css">
        <link rel="stylesheet" href="css/ui-box.css">
        <link rel="stylesheet" href="css/ui-base.css">
        <link rel="stylesheet" href="css/ui-color.css">
        <link rel="stylesheet" href="css/appcan.icon.css">
        <link rel="stylesheet" href="css/appcan.control.css">
        <link rel="stylesheet" href="info_content/css/main.css">
    </head>
    <body class="um-vp bc-bg" ontouchstart>
        <div class="umar-a" id="">
            <div id="tabview" class="uf sc-bg ubb bc-border" id=""></div>
            <div id="goodsList" class="ub-f1 tx-l"></div>

        </div>
        <script src="js/appcan.js"></script>
        <script src="js/appcan.control.js"></script>
        <script src="js/appcan.listview.js"></script>
        <script src="js/appcan.tab.js"></script>
    </body>
    <script>
        var build_id;
        var build_name;
        var html = ['http://whynothappy.sinaapp.com/getOrderListForSellBuilding', 'http://whynothappy.sinaapp.com/getOrderListForBuyBuilding']
        var key_state = ['2', '3'];
        var buyData = [];
        var sellData = [];
        var now_index;
        var confirm_html = 'http://whynothappy.sinaapp.com/Confirm';
        var username;
        var password;
        
        appcan.ready(function() {
            appcan.initBounce();
            now_index = 0;
            build_id = localStorage.getItem('build_id');
            build_name = localStorage.getItem('build_name');
            username = 'postman';
            password = 'postman';
if (build_id == null) build_id = "1";  // for debug
            var request = appcan.require('request');
            request.ajax({
                type : 'POST',
                url : html[0],
                data : {building_id: build_id},
                contentType: 'application/json',
                success:function(data){
                    data = eval("(" + data + ")");
                    init(data.orders, key_state[0]);
                },
                error:function(){
                    alert("fuck");
                }
            })
        })
       
        var tabview = appcan.tab({
            selector : "#tabview",
            hasIcon : false,
            hasAnim : true,
            hasLabel : true,
            hasBadge : false,
            data : [{
                label : "要拿的",
            }, {
                label : "要送的",
            }]
        });
        tabview.on("click", function(obj, index) {
            now_index = index;
            if (index == 0)
            {
                var request = appcan.require('request');
                request.ajax({
                    type : 'POST',
                    url : html[index],
                    data : {building_id: build_id},
                    contentType: 'application/json',
                    success:function(data){
                        data = eval("(" + data + ")");
                        init(data.orders, key_state[index]);
                    },
                    error:function(){
                        alert("fuck");
                    }
                })
            }
            else
            if (index == 1)
            {
                var request = appcan.require('request');
                request.ajax({
                    type : 'POST',
                    url : html[index],
                    data : {building_id: build_id},
                    contentType: 'application/json',
                    success:function(data){
                        data = eval("(" + data + ")");
                        init(data.orders, key_state[index]);
                    },
                    error:function(){
                        alert("fuck");
                    }
                })
            }
        })
        
        var arrData = [];
        function init(data, key) {
            arrData = [];
            showGoods();
            var len = data.length;
            for(var i = 0; i < len; ++i) if (data[i].goods_state == key)
            {
                arrData.push({
                    "bookname":data[i].title, 
                    "img":data[i].pic_url, 
                    "price":"￥"+data[i].price, 
                    "time": data[i].time,
                    "goods_id": data[i].goods_id,
                    });
            }
            arrData.sort(down);
            showGoods();
        }
        
        function changeState(key)
        {
            var tmp = arrData;
            var len = arrData.length;
            arrData = [];
            for(var i = 0; i < len; ++i) if (tmp[i].goods_id != key)
            {
                arrData.push({
                    "bookname":tmp[i].bookname, 
                    "img":tmp[i].img, 
                    "price":"￥"+tmp[i].price, 
                    "time": tmp[i].time,
                    "goods_id": tmp[i].goods_id,
                    });
            }
            arrData.sort(down);
            showGoods();
        }
        
        function down(x, y) {
            return (x.time > y.time) ? 1 : -1
        }
        function showGoods() {
            
            var listData = [];
            var len = arrData.length;
            for (var i = 0; i < len; i++) {
                var list = {
                    title : arrData[i].bookname,
                    icon : arrData[i].img,
                    goods_id : arrData[i].goods_id,
                }
                listData.push(list);
            }
            var lv1 = appcan.listview({
                selector : "#goodsList",
                type : "thickLine",
                hasIcon : true,
                hasAngle : true,
                hasSubTitle : false,
                multiLine : 1,
                hasCheckbox : false,
                align : 'left'
            });
            lv1.set(listData);
            
            lv1.on('click', function(ele, context, obj, subobj) {
                if (now_index == 0)
                {
                    changeState(context.goods_id);
                    var request = appcan.require('request');
                    request.ajax({
                        type : 'POST',
                        url : confirm_html,
                        data : {username: username, password: password, goods_id: context.goods_id},
                        contentType: 'application/json',
                        success:function(data){
                            changeState(goods_id);
                        },
                        error:function(){
                            alert("fuck");
                        }
                    })
                }
            })
        }
    </script>
</html>
